// pages/resetpassword/resetpassword.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height,
    codeNumber: 0,
    condetime: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  //返回上一页
  out() {
    wx.navigateBack({
      delta: 1,
    })
  },
  oninput(e) {
    
    let data = e.detail.value
    let type = e.currentTarget.dataset.type
    this.setData({
      [type]: data
    })
  },
  ontime() {
    let _this = this
    let phone = _this.data.phone
    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let condetime = _this.data.condetime
    if (condetime == 60) {
      $api.sendSmsCode({
        mobile: phone,
        code_type: 3
      }).then((res) => {
        // _this.setData({
        //   code_type: res.data.data.code_type
        // })
        _this.timeOut()
      }).catch((err) => [
        _this.setData({
          loadModal: false
        })
      ])
    } else {

    }

  },
  timeOut(e) {
    let _this = this
    let condetime = _this.data.condetime
    console.log(condetime)
    if (condetime > 0) {
      setTimeout(() => {
        condetime--;
        _this.setData({ condetime })
        _this.timeOut()
      }, 1000)
    } else {
      _this.setData({ condetime: 60 })
    }
  },
  sumb() {
    let _this = this
    let phone = _this.data.phone
    let code = _this.data.code
    let new_password = _this.data.new_password
    let new_password_two = _this.data.new_password_two
    console.log(phone,code,new_password,new_password_two,)
    if (!phone || !code || !new_password || !new_password_two) {
      wx.showToast({
        title: '请将信息填写完整',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (new_password != new_password_two) {
      wx.showToast({
        title: '请将密码填写一致',
        icon: 'none',
        duration: 2000
      })
      return
    }
    $api.editavatar({
      new_password: new_password,
      code: code,
      login_token: wx.getStorageSync('login_token'),
      mobile:phone
    }).then((res) => {
      if (res.data.code == 0) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 500)

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
      _this.timeOut()
    }).catch((err) => [
      _this.setData({
        loadModal: false
      })
    ])

  }
})